package day22.HomeworkCollections;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Menu {

	private static final String MSG_INDEX_OF_THE_PRODUCT_TO_SEE = "Which is the index of the product you wish to see?";
	private static final String MSG_NOT_VALID_INDEX = "This is not a valid index.";
	private static final String MSG_ENTER_INDEX_OF_PRODUCT_TO_BE_REMOVED = "Enter the index of the product, which you wish to be removed:";
	private static final String MSG_NOT_A_VALID_PRICE = "This is not a valid price.";
	private static final String MSG_ENTER_PRICE_PRODUCT = "Please enter the price of the product:";
	private static final String MSG_NOT_A_VALID_MEASURING_UNIT = "This is not a valid measuring unit.";
	private static final String MSG_ENTER_MEASURING_UNITS = "Please enter the measuring units of your product:";
	private static final String MSG_NOT_A_VALID_QUANTITY = "This is not a valid quantity.";
	private static final String MSG_ENTER_QUANTITY_OF_PRODUCT = "Please enter quantity of your product: ";
	private static final String MSG_NOT_A_VALID_PRODUCT_NAME = "This is not a valid product name.";
	private static final String MSG_ENTER_NAME_OF_PRODUCT = "Please enter name of your product: ";
	private static final int EXIT_VALUE = 6;
	private static final int OPTIONS_MAX_NUM = 6;
	
	private static Scanner inputS = new Scanner(System.in);

	public static void start() {
		Scanner in = new Scanner(System.in);
		int input = -1;
		ShoppingList newList = new ShoppingList();

		while (input != EXIT_VALUE) {
			Menu.printMenu();

			try {
				input = in.nextInt();
			} catch (InputMismatchException e) {
				System.out.println("This is not a valid option.");
				input = -2;
				continue;
			}

			Menu.inputOption(input, newList);
			System.out.println();
		}

	}

	private static void createAndAddNewProduct(ShoppingList shoppingList) {

		String pName;
		int pQuantity;
		String pMeasurementUnit;
		double pUnitPrice;
		
		

		System.out.println(MSG_ENTER_NAME_OF_PRODUCT);
		try {
			Scanner input = new Scanner(System.in);
			pName = input.nextLine();
		} catch (InputMismatchException e) {
			System.out.println(MSG_NOT_A_VALID_PRODUCT_NAME);
			return;
		}

		System.out.println(MSG_ENTER_QUANTITY_OF_PRODUCT);
		try {
			Scanner input = new Scanner(System.in);
			pQuantity = input.nextInt();
			//input.nextLine();
		} catch (InputMismatchException e) {
			System.out.println(MSG_NOT_A_VALID_QUANTITY);
			return;
		}

		System.out.println(MSG_ENTER_MEASURING_UNITS);
		try {
			Scanner input = new Scanner(System.in);
			pMeasurementUnit = input.nextLine();
		} catch (InputMismatchException e) {
			System.out.println(MSG_NOT_A_VALID_MEASURING_UNIT);
			return;
		}

		System.out.println(MSG_ENTER_PRICE_PRODUCT);
		try {
			Scanner input = new Scanner(System.in);
			pUnitPrice = input.nextDouble();
			//input.nextLine();
		} catch (InputMismatchException e) {
			System.out.println(MSG_NOT_A_VALID_PRICE);
			return;
		}

		Product newProduct = new Product(pName, pQuantity, pMeasurementUnit,
				pUnitPrice);
		shoppingList.add(newProduct);
		return;
	}
	
	private static void removeItem(ShoppingList shoppingList){
		
		int index;
		System.out
				.println(MSG_ENTER_INDEX_OF_PRODUCT_TO_BE_REMOVED);
		try{
			Scanner input = new Scanner(System.in);
			index = input.nextInt();
		} catch (InputMismatchException e){
			System.out.println(MSG_NOT_VALID_INDEX);
			return;
		}
		shoppingList.remove(index);
		return;
	}
	
	private static void printProduct(ShoppingList shoppingList){
		System.out.println(MSG_INDEX_OF_THE_PRODUCT_TO_SEE);
		int index;
	
		
		try{
			Scanner input = new Scanner(System.in);
			index = input.nextInt();
		} catch (InputMismatchException e){
			System.out.println(MSG_NOT_VALID_INDEX);
			return;
		}
		
		System.out.println(shoppingList.toString());
		
	}

	public static void printMenu() {
		System.out.println("Please choose an option:");
		System.out.println("1: Add a new item");
		System.out.println("2: Remove item");
		System.out.println("3: Get item No: ");
		System.out.println("4: See list of items");
		System.out.println("5: Clear shopping list");
		System.out.println("6: Exit");

	}

	public static void inputOption(int optNum, ShoppingList shoppingList) {

		if (shoppingList == null) {
			System.out.println("No such shopping list.");
			return;
		}

		if (0 < optNum && optNum <= OPTIONS_MAX_NUM) {

			switch (optNum) {

			case 1:
				Menu.createAndAddNewProduct(shoppingList);
				break;
			case 2:
				//Menu.removeItem(shoppingList);
				
				System.out.println("Which item would you like to remove?");
				
				try {
					Scanner input = new Scanner(System.in);
					String pName = input.nextLine();
					Product product = new Product(pName);
					shoppingList.remove(product);
				} catch (InputMismatchException e) {
					System.out.println(MSG_NOT_A_VALID_PRODUCT_NAME);
					return;
				}
				break;
			case 3:
				Menu.printProduct(shoppingList);
				break;
			case 4:
				shoppingList.printList();
				break;
			case 5:
				shoppingList.clear();
				break;
			case 6:
				System.out
						.println("You've finnished operating with this shopping list.");
				break;

			}

		}

	}

}
